package com.dinree.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;

import com.base.library.TopBarBaseActivity;
import com.base.library.widget.CompetitionWebChromeClient;
import com.dinree.R;
import com.dinree.bean.UserBean;
import com.dinree.databinding.ActivityCouponWebBinding;
import com.dinree.utils.Constant;
import com.dinree.utils.LaunchUtil;
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import java.util.HashMap;
import java.util.Map;

public class CouponWebActivity extends TopBarBaseActivity {
    private ActivityCouponWebBinding activityCouponWebBinding;
    private UserBean userBean;

    @Override
    public int getContentView() {
        return R.layout.activity_coupon_web;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        activityCouponWebBinding = getContentViewBinding();
        userBean = realm.findFirst(UserBean.class);
        setTopLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (activityCouponWebBinding.webView.canGoBack()) {
                    activityCouponWebBinding.webView.goBack();
                } else {
                    finish();
                }
            }
        });
        setTopRightButton("已使用", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/selfcoupon?q=used");
            }
        });
        String url = getIntent().getStringExtra("url");
        activityCouponWebBinding.webView.addJavascriptInterface(new JSHook(), "dinree");
        if (userBean != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("user_id", userBean.user_id);
            map.put("user_token", userBean.user_token);
            map.put("referer", "https://web.dinree.cn/epsea/front");
            activityCouponWebBinding.webView.loadUrl(url, map);
        } else {
            activityCouponWebBinding.webView.loadUrl(url);
        }
        activityCouponWebBinding.webView.setWebChromeClient(new CompetitionWebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);

            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitle(title);
            }

            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissionsCallback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }

            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                super.onShowCustomView(view, customViewCallback);
            }
        });
    }

    public class JSHook {
        @JavascriptInterface
        public void close() {
            finish();
        }
    }
}

